package csc.daonjpa.java.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Component;
import csc.daonjpa.java.domain.Card;
import csc.daonjpa.java.transaction.TransactionManager;

@Component
public class CardDAO {
	/**
	 * @return all cards from Card table
	 */
	public List<Card> getCards() {
		EntityManager em = TransactionManager.getEntityManager();
		TypedQuery<Card> query = em.createQuery("SELECT a FROM Card a", Card.class);
		List<Card> cards = null;
		cards = query.getResultList();
		return cards;
	}
	/**
	 * 
	 * @param id_customer
	 * @return all cards by id_customer from database
	 */
	public List<Card> getCardByCustomer (Long id_customer) {
		EntityManager em = TransactionManager.getEntityManager();
		TypedQuery<Card> query = em.createQuery("SELECT a FROM Card a WHERE a.customer.id = :id_customer", Card.class);
		query.setParameter("id_customer", id_customer);
		List<Card> cards = null;
		cards = query.getResultList();
		return cards;
	}
	/**
	 * Add card to Card table
	 * @param card Card entity that contains mapped with each column in Card table
	 * @return
	 */
	public boolean addCard(Card card) {
		EntityManager em = TransactionManager.getEntityManager();
		em.persist(card);
		return true;
	}
}
